package com.shengyo.mobile.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.yuxiong.utils.DownloadException;
import org.yuxiong.utils.SuperDate;

import com.shengyo.mobile.model.bean.Mobile;


public class MobileDAO extends BaseDAO{

	public static List<Mobile> findAll(){
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Mobile> rtnList = new ArrayList();
		Connection conn = null;
		String sql ="select * from mobile";
		try {
			conn = getConnection();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();

			while(rs.next()){
				Mobile m = new Mobile();
				m.setId(rs.getInt(1));
				m.setMobile(rs.getString(2));
				m.setLinkman(rs.getString(3));
				m.setMobileZone(rs.getString(4));
				m.setProvice(rs.getString(5));
				m.setCity(rs.getString(6));
				m.setMobileCompany(rs.getString(7));
				m.setPostalcode(rs.getString(8));
				m.setPhoneZone(rs.getString(9));
				m.setFromSite(rs.getString(10));
				m.setFromPage(rs.getString(11));
				m.setCreateTime(rs.getTimestamp(12));
				rtnList.add(m);		
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rtnList;
	}
	
	public static List<Mobile> findAll(int first, int max){
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		List<Mobile> rtnList = new ArrayList();
		Connection conn = null;
		String sql ="select * from mobile limit ?,?";
		try {
			conn = getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, first);
			pstmt.setInt(2, max);
			rs = pstmt.executeQuery();

			while(rs.next()){
				Mobile m = new Mobile();
				m.setId(rs.getInt(1));
				m.setMobile(rs.getString(2));
				m.setLinkman(rs.getString(3));
				m.setMobileZone(rs.getString(4));
				m.setProvice(rs.getString(5));
				m.setCity(rs.getString(6));
				m.setMobileCompany(rs.getString(7));
				m.setPostalcode(rs.getString(8));
				m.setPhoneZone(rs.getString(9));
				m.setFromSite(rs.getString(10));
				m.setFromPage(rs.getString(11));
				m.setCreateTime(rs.getTimestamp(12));
				rtnList.add(m);		
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return rtnList;
	}
	
	public static Mobile findByMobile(String mobile){
		PreparedStatement pstmt = null;
		String sql ="SELECT * FROM mobile where mobile=?";
		Mobile m = null;
		Connection conn = null;
		try {
			conn = getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, mobile);
			
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				m = new Mobile();
				m.setId(rs.getInt(1));
				m.setMobile(rs.getString(2));
				m.setLinkman(rs.getString(3));
				m.setMobileZone(rs.getString(4));
				m.setProvice(rs.getString(5));
				m.setCity(rs.getString(6));
				m.setMobileCompany(rs.getString(7));
				m.setPostalcode(rs.getString(8));
				m.setPhoneZone(rs.getString(9));
				m.setFromSite(rs.getString(10));
				m.setFromPage(rs.getString(11));
				m.setCreateTime(rs.getTimestamp(12));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return m;
	} 
	
	public static boolean isExistedPage(String fromPage){
		PreparedStatement pstmt = null;
		String sql ="SELECT id FROM mobile where fromPage=?";
		Connection conn = null;
		try {
			conn = getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, fromPage);
			
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
	
	public static Mobile save(Mobile mobile){
		PreparedStatement pstmt = null;
		Connection conn = null;
		String sql ="INSERT INTO mobile (mobile, linkman, mobileZone, provice, city, mobileCompany, postalcode, phoneZone, fromSite, fromPage, createTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			conn = getConnection();
			pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			pstmt.setString(1, mobile.getMobile());
			pstmt.setString(2, mobile.getLinkman());
			pstmt.setString(3, mobile.getMobileZone());
			pstmt.setString(4, mobile.getProvice());
			pstmt.setString(5, mobile.getCity());
			pstmt.setString(6, mobile.getMobileCompany());
			pstmt.setString(7, mobile.getPostalcode());
			pstmt.setString(8, mobile.getPhoneZone());
			pstmt.setString(9, mobile.getFromSite());
			pstmt.setString(10, mobile.getFromPage());
			pstmt.setTimestamp(11, new Timestamp(new Date().getTime()));
			if(pstmt.executeUpdate() == 1){
				ResultSet rs = pstmt.getGeneratedKeys();
				if(rs!=null){
					rs.next();
					mobile.setId(rs.getInt(1));
				}
			}	
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			try {
				if(pstmt != null) pstmt.close();
				if(conn != null) conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return mobile;
	} 
	
	public static void main(String[] args) {
		System.out.println(new SuperDate().getMonth());
	}
}

